package org.NooLab.itexx.app.config;

import java.io.File;
import java.io.Serializable;


import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.storage.StorageProvider;
import org.NooLab.itexx.stuctures.GuiPersistenceData;
import org.NooLab.utilities.files.DFutils;




public class ITexxAppProperties 
									implements 
												ITexxAppPropertiesIntf,
												Serializable{

	
	private static final long serialVersionUID = -2273587827503254012L;

	
	public static final int _MAX_PAGES_SearchEngine = 100;
	
	transient ITexxWindowCommons appCommons;
	
	ITexxIniPreferences iniPreferences ;
	
	String ownBinPath="";
	String ownAppPath="";
	String configFilename="";

	private String jarFileName="";

	private GuiPersistenceData guiData;
	private Class classOfMain;

	private String appNameShort="";

	// note that we have the browser box as the last one !
	private int numberOfPreparedCommboxes = 5;

	private int commPortBase = 7101;

	private String preferencesIniFilepath="";

	private boolean consolidateLingWordSets;
	private boolean earlyPreprocessing=true;
	private boolean allowForTransparency = true;
	private boolean alwaysOnTop = false;

	private String panelsCssFilepath="";
	private boolean panelsCssAllowed ;


	private int fullFrameHeight;

	
	static int maxNumberOfSearchEnginePages = _MAX_PAGES_SearchEngine;

	
	// ========================================================================
	public ITexxAppProperties(){
		
		iniPreferences = new ITexxIniPreferences(this);
		
	}
	// for cloning
	public ITexxAppProperties(ITexxAppProperties inprops) {
	
		
		appCommons = inprops.appCommons  ;
		
		ownBinPath = inprops.ownBinPath  ;
		ownAppPath = inprops.ownAppPath  ;
		configFilename = inprops.configFilename  ;

		
		earlyPreprocessing = inprops.earlyPreprocessing  ;
		allowForTransparency = inprops.allowForTransparency  ;
		alwaysOnTop = inprops.alwaysOnTop  ;
		guiData = inprops.guiData  ;
		appNameShort = inprops.appNameShort  ;
		numberOfPreparedCommboxes = inprops.numberOfPreparedCommboxes  ;
		commPortBase = inprops.commPortBase  ;
		
		
	}
	// ========================================================================
	
	public static ITexxAppPropertiesIntf getInstance(String basePath) {
		
		ITexxAppProperties props = new ITexxAppProperties();
		
		// destatify the string variables...
		CareForPathVariables cp = new CareForPathVariables(props, basePath);
		String filename = props.getConfigFilename() ;
		
		if (DFutils.fileExists(filename)==false){
			System.out.println("properties file not found, proceeding with default settings...");
			return props;
		}
		
		// ensure defaults of path variables, in case the loading fails these are empty and have to be recocvered
		String _binPath,_appPath;
		_binPath = props.ownBinPath ;
		_appPath = props.ownAppPath ;
		
		try{
			StorageProvider stor = new StorageProvider();
			
			ITexxAppProperties props2 = (ITexxAppProperties) stor.loadStoredObject( props.getConfigFilename() ) ;
		
			
			if (props2!=null){
				props = props2;
			}else{
				System.out.println("laoding properties file failed, proceeding with default settings...");
				
				props2 = new ITexxAppProperties(props);
				
				props2.setConfigFilename( props.getConfigFilename());
				
				
				stor.storeObject(props2, props.getConfigFilename());
				props = props2;
			}
			
			
		}catch(Exception e){
			System.err.println("laoding properties file failed (wrong format), proceeding with default settings...\n");
			e.printStackTrace() ;
		}

		if (DFutils.folderExists(props.getOwnBinPath())==false){
			props.setOwnBinPath( _binPath );
		}
		if ((DFutils.folderExists(props.getOwnAppPath())==false) | (props.getOwnAppPath().toLowerCase().contains("muse")==false)){
			props.setOwnAppPath( _appPath); //
			
			String cfgFilename = props.getConfigFilename();
			cfgFilename = (new File(cfgFilename)).getName() ;
			if (cfgFilename.toLowerCase().contains("muse")==false){
				cfgFilename = "musewin.cfg";
			}
			cfgFilename = DFutils.createPath( _appPath, cfgFilename ) ;
			props.setConfigFilename(cfgFilename);
		}

		
		return props;
	}

	
	@Override
	public String getOwnBinPath() {
		if (DFutils.folderExists(ownAppPath)==false){
			ownBinPath = "";
		}
		return ownBinPath;
	}


	@Override
	public void loadFromFile( String filename){
		this.configFilename = filename;
	}

	@Override
	public void save(){
		saveToFile( configFilename );
	}
	
	
	public void saveToFile(String filename){
		
		try{
			
			// appCommons.getStorage().write ... 			configFilename;
			
			
		}catch(Exception e){
			
		}
	}


	@Override
	public void loadPreferences() {
		// 
		String ppath = getPreferencesIniFilepath();
		
		if (iniPreferences==null){
			iniPreferences = new ITexxIniPreferences(this);
		}
		iniPreferences.loadFromFile(ppath);
		// take some of the measures ?
		
		
		
	}
	
	// ------------------------------------------------------------------------
	public boolean isEarlyPreprocessing() {
		return earlyPreprocessing;
	}

	public void setEarlyPreprocessing(boolean earlyPreprocessing) {
		this.earlyPreprocessing = earlyPreprocessing;
	}


	public static int getMaxNumberOfSearchEnginePages() {
		return maxNumberOfSearchEnginePages;
	}
	public int getMaxNumberOfSearchenginePages() {
		return maxNumberOfSearchEnginePages;
	}

	public void setMaxNumberOfSearchEnginePages(int maxNumValue) {
		maxNumberOfSearchEnginePages = maxNumValue;
	}


	public boolean isAllowForTransparency() {
		return allowForTransparency;
	}


	public void setAllowForTransparency(boolean allowForTransparency) {
		this.allowForTransparency = allowForTransparency;
	}


	@Override
	public boolean getAlwaysOnTop() {
		// 
		return alwaysOnTop;
	}

	@Override
	public void setAlwaysOnTop(boolean alwaysOnTop) {
		this.alwaysOnTop = alwaysOnTop;
	}


	public void setGuiData(GuiPersistenceData guidata) {
		// clone...
		guiData = new GuiPersistenceData(guidata);
	}

	public GuiPersistenceData getGuiData() {
		return guiData;
	}

	@Override
	public String getConfigFilename() {
		return configFilename;
	}


	public void setConfigFilename(String configFilename) {
		this.configFilename = configFilename;
	}



	public void setOwnBinPath(String ownBinPath) {
		if (DFutils.folderExists(ownBinPath)==false){
			ownBinPath = "";
		}
		this.ownBinPath = ownBinPath;
	}

	@Override
	public String getOwnAppPath() {
		return ownAppPath;
	}


	public void setOwnAppPath(String ownAppPath) {
		this.ownAppPath = ownAppPath;
	}


	@Override
	public void setCommonsReference(ITexxWindowCommons commons) {
		
		appCommons = commons;
	}

	@Override
	public String getAppNameShort() {
		// 
		return appNameShort;
	}
	@Override
	public void setAppNameShort(String appname) {
		// 
		appNameShort = appname;
	}

	@Override
	public int getNumberOfPreparedCommboxes() {
		// 
		if (numberOfPreparedCommboxes==0){
			numberOfPreparedCommboxes=5;
		}
		return numberOfPreparedCommboxes;
	}

	@Override
	public int getCommPortBase() {
		// 
		if (commPortBase<1100){
			commPortBase = 7071 ;
		}
		return commPortBase;
	}

	public void setNumberOfPreparedCommboxes(int numberOfPreparedCommboxes) {
		this.numberOfPreparedCommboxes = numberOfPreparedCommboxes;
	}

	public void setCommPortBase(int commPortBase) {
		this.commPortBase = commPortBase;
	}

	@Override
	public boolean consolidateLingWordSets() {
		return consolidateLingWordSets;
	}
	@Override
	public void setConsolidateLingWordSets(boolean flag) {
		consolidateLingWordSets = flag;
	}
	
	
	@SuppressWarnings("rawtypes")
	@Override
	public Class getClassOfMain() {
		
		return classOfMain;
	}
	@Override
	public void setClassOfMain(Class clzz) {
		classOfMain = clzz;
	}
	@Override
	public String getJarFileName() {
		// 
		return jarFileName;
	}
	@Override
	public void setJarFileName(String jarname) {
		// 
		jarFileName = jarname;
	}
	
	
	/**
	 * provide only the name! the config sub-dir will be used
	 */
	@Override
	public void setPanelsCssFilepath(String filename) {
		String filepath = "";
		
		filepath = configFilename; 
		filepath = DFutils.getParentDir(configFilename) ;
		filepath = DFutils.createPath(filepath, filename);
		
		panelsCssFilepath = filepath;
	}
	@Override
	public String getPanelsCssFilepath() {
		//
		
		return panelsCssFilepath;
	}
	
	@Override
	public void setPanelsCssAllowed(boolean flag) {
		
		panelsCssAllowed = flag;
	}
	
	@Override
	public boolean isPanelsCssAllowed() {
		boolean rB = panelsCssAllowed;
		
		if (rB){
			rB = DFutils.fileExists( panelsCssFilepath );
		}
			
		return rB;
	}
	@Override
	public void setPreferencesIniFilepath(String filename) {
		String filepath = "";
		
		filepath = configFilename; 
		filepath = DFutils.getParentDir(configFilename) ;
		filepath = DFutils.createPath(filepath, filename);
		
		preferencesIniFilepath = filepath;
		
	}
	@Override
	public String getPreferencesIniFilepath() {
		// 
		return preferencesIniFilepath;
	}
	
	@Override
	public ITexxIniPreferences getIniPreferences() {
		if (iniPreferences==null){
			iniPreferences = new ITexxIniPreferences(this);
			loadPreferences();
		}
		return iniPreferences;
	}
	
	public void setIniPreferences(ITexxIniPreferences iniPreferences) {
		this.iniPreferences = iniPreferences;
	}
	
	
	@Override
	public void setFullFrameHeight(int height) {
		// 
		fullFrameHeight  =height;
	}
	@Override
	public int getFullFrameHeight() {
		// 
		return fullFrameHeight;
	}
	
	
}



class CareForPathVariables{
	
	public CareForPathVariables(ITexxAppProperties props, String basePath){

		if (basePath.contains("/dev/java/")){
			basePath = "D:/data/iTexx/bin/" ;
		}
		
		if (basePath.contains("/bin")==false){
			basePath = DFutils.createPath(basePath, "/bin/");
		}
		props.ownBinPath = basePath;
		props.ownAppPath = DFutils.createPath( DFutils.getParentDir(basePath), "app/MuseGui/config");
		// load properties from file

		props.configFilename = DFutils.createPath(props.ownAppPath, "musewin.cfg");

	}
}
